package com.facebank.usersupport.service.impl;

import com.facebank.usersupport.dto.OrderDto;
import com.facebank.usersupport.mapper.usersupport.usersupport.*;
import com.facebank.usersupport.model.*;
import com.facebank.usersupport.service.IMallService;
import com.facebank.usersupport.util.SessionUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.List;

@Service
public class MallServiceImpl implements IMallService{
    @Autowired
    private ActivityMapper activityMapper;
    @Autowired
    private PeopleRemainMapper peopleRemainMapper;
    @Autowired
    private DgutCartMapper dgutCartMapper;
    @Autowired
    private OrderProductMapper orderProductMapper;
    @Autowired
    private DgutMessageMapper dgutMessageMapper;

    @Override
    public RestModel up(Integer id) {
        Activity activitybase = activityMapper.selectByPrimaryKey(id);
        PeopleRemain peopleRemain = new PeopleRemain();
        peopleRemain.setActivityId((long)id);
        peopleRemain.setCreateTime(System.currentTimeMillis());
        peopleRemain.setPeopleRemain(activitybase.getStock());
        int is_successpre =peopleRemainMapper.insertSelective(peopleRemain);
        if (is_successpre!=1){return new RestModel();}
        Activity activity = new Activity();
        SimpleDateFormat df = new SimpleDateFormat("MM");
        activity.setMonthNo(Integer.valueOf(df.format(System.currentTimeMillis())));
        activity.setId(id);
        activity.setStatus((short)1);
        activity.setUpdateTime(System.currentTimeMillis());
        int is_success = activityMapper.updateByPrimaryKeySelective(activity);
        if (is_success==1){return new RestModel(RestModel.CODE_SUCCESS, RestModel.MESSAGE_SUCCESS);}
        return new RestModel();
    }

    @Override
    public RestModel down(Integer id) {
        Activity activity = new Activity();
        activity.setId(id);
        activity.setStatus((short)2);
        int is_success = activityMapper.updateByPrimaryKeySelective(activity);
        if (is_success==1){return new RestModel(RestModel.CODE_SUCCESS, RestModel.MESSAGE_SUCCESS);}
        return new RestModel();
    }

    @Override
    public RestModel cart(Long assId, Integer activityId,Integer num) {
        DgutCart dgutCart = new DgutCart();
        dgutCart.setActivityId(activityId);
        dgutCart.setAssociatorId(assId);
        dgutCart.setNumber(num);
        dgutCart.setCreateTime(System.currentTimeMillis());
        int is_success =dgutCartMapper.insertSelective(dgutCart);
        if (is_success==1){return new RestModel(RestModel.CODE_SUCCESS, RestModel.MESSAGE_SUCCESS);}
        return new RestModel();
    }

    @Override
    public RestModel orderList(Integer pageNo, Integer length, Long orderNo, Short routeType, Short status, Long startTime, Long endTime) {
        List<OrderDto> orderDtos = orderProductMapper.orderList(orderNo,routeType,status,startTime,endTime);
        PageHelper.startPage(pageNo,length);
        PageInfo<OrderDto> pageInfo =new PageInfo<>(orderDtos);
        return new RestModel(pageInfo);
    }

    @Override
    public RestModel ahowMessage() {
       List<DgutMessage> dgutMessages = dgutMessageMapper.showMessage();
        return new RestModel(dgutMessages);
    }

    @Override
    public RestModel addMessage(DgutMessage dgutMessage, HttpSession session) {
        Long createId = Long.valueOf(SessionUtil.getUserId(session));
        dgutMessage.setCreaterId(createId);
        dgutMessage.setGtmCreate(System.currentTimeMillis());
        dgutMessage.setGtmUpdate(System.currentTimeMillis());
        int is_success = dgutMessageMapper.insertSelective(dgutMessage);
        return new RestModel("添加成功");
    }

    @Override
    public RestModel updateMessage(DgutMessage dgutMessage, HttpSession session) {
        Long createId = Long.valueOf(SessionUtil.getUserId(session));
        dgutMessage.setCreaterId(createId);
        dgutMessage.setGtmUpdate(System.currentTimeMillis());
        int is_success = dgutMessageMapper.updateByPrimaryKeySelective(dgutMessage);
        return new RestModel("更新成功");
    }

    @Override
    public RestModel deleteMessage(Long messId) {
        int is_success = dgutMessageMapper.deleteByPrimaryKey(messId);
        return new RestModel("删除成功");
    }

    @Override
    public RestModel detailMessage(Long messId) {
        DgutMessage dgutMessage = dgutMessageMapper.selectByPrimaryKey(messId);
        return new RestModel(dgutMessage);
    }

}
